package com.tz.fruit.service.impl;

import com.tz.fruit.dao.IProductDao;
import com.tz.fruit.entity.*;
import com.tz.fruit.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * Created by CHEN on 2017/7/19.
 */

@Service
public class ProductServiceImpl implements IProductService{
    @Autowired
    private IProductDao productDao;

    @Override
    public void save(Product p) {
        productDao.save(p);
    }

    @Override
    public PageBean<Product> findByPage(String keyWord, RecState recState, Category category,String loc,boolean order, Long pageNow, Long pageSize) {
        PageBean<Product> page=new PageBean<>();
        page.setList(productDao.findAll(keyWord,recState,category,loc,order,pageNow,pageSize));
        page.setPageNow(pageNow);
        page.setPageSize(pageSize);
        Long rowCount=productDao.findRows(keyWord,recState,category,loc);
        Long pageCount=rowCount/pageSize;
        if (rowCount%pageSize!=0)
            pageCount++;
        page.setPageCount(pageCount);
        return page;
    }

    @Override
    public void save(Intro intro) {
        productDao.save(intro);
    }

    @Override
    public void delByIntroId(Long introId) {
        productDao.delByIntroId(introId);
    }

    @Override
    public void update(Intro intro) {
        productDao.update(intro);
    }

    @Override
    public void update(Product p) {
        productDao.update(p);
    }

    @Override
    public Product findById(Long pid) {
        return productDao.findById(pid);
    }

    @Override
    public Product findByPNo(String pNo) {
        return productDao.findByPNo(pNo);
    }

    @Override
    public Product findByPName(String pName) {
        return productDao.findByPName(pName);
    }

    @Override
    public void save(Category c) {
        productDao.save(c);
    }

    @Override
    public void delByCategoryId(Long categoryId) {
        productDao.delByCategoryId(categoryId);
    }

    @Override
    public void update(Category c) {
        productDao.update(c);
    }

    @Override
    public List<Category> findAllCategory(String categoryNo,String categoryName) {
        List<Category> categories=productDao.findAllCategory(categoryNo,categoryName);
        Collections.sort(categories, new Comparator<Category>() {
            @Override
            public int compare(Category o1, Category o2) {
                if (o1.getCategoryId()>o2.getCategoryId())
                    return 1;
                if (o1.getCategoryId()<o2.getCategoryId())
                    return -1;
                return 0;
            }
        });
        return categories;
    }

    @Override
    public Category findByCategoryId(Long categoryId) {
        return productDao.findByCategoryId(categoryId);
    }

    @Override
    public Category findByCategoryNo(String categoryNo) {
        return productDao.findByCategoryNo(categoryNo);
    }

    @Override
    public Category findByCategoryName(String categoryName) {
        return productDao.findByCategoryName(categoryName);
    }

    @Override
    public void delById(Long pid) {
        productDao.delById(pid);
    }

    @Override
    public List<Intro> findIntroByPid(Long pid) {
        return productDao.findIntroByPid(pid);
    }

    @Override
    public Intro findByIntroId(Long introId) {
        return productDao.findByIntroId(introId);
    }

    @Override
    public Intro findByKey(String key) {
        return productDao.findByKey(key);
    }

    @Override
    public void save(Comment comment) {
        productDao.save(comment);
    }

    @Override
    public void delByCommmentId(Long commmentId) {
        productDao.delByCommmentId(commmentId);
    }

    @Override
    public void update(Comment comment) {
        productDao.update(comment);
    }

    @Override
    public List<Image> findImgByPid(Long pid) {
        return productDao.findImgByPid(pid);
    }

    @Override
    public Image findByImageId(Long imageId) {
        return productDao.findByImageId(imageId);
    }

    @Override
    public List<Comment> findCommentByPid(Long pid) {
        return productDao.findCommentByPid(pid);
    }

    @Override
    public Comment findByCommmentId(Long commmentId) {
        return productDao.findByCommmentId(commmentId);
    }

    @Override
    public void save(Image image) {
        productDao.save(image);
    }

    @Override
    public void delByImageId(Long imageId) {
        productDao.delByImageId(imageId);
    }

    @Override
    public void update(Image image) {
        productDao.update(image);
    }

    @Override
    public PageBean<Comment> findCommentByPage(Long id, Long pageNow, Long pageSize) {
        PageBean<Comment> page=new PageBean<>();
        page.setList(productDao.findAllC(id,pageNow,pageSize));
        page.setPageNow(pageNow);
        page.setPageSize(pageSize);
        Long rowCount=productDao.findRowsC(id);
        Long pageCount=rowCount/pageSize;
        if (rowCount%pageSize!=0)
            pageCount++;
        page.setPageCount(pageCount);

        return page;
    }

    @Override
    public Product findProductById(Long id) {
        return productDao.findProductById(id);
    }

    @Override
    public Recent saveToRecent(Recent recent, Product product) {

        LinkedList<Product> products=null;
        if(recent==null){
            products=new LinkedList<>();
            products.addFirst(product);
            recent=new Recent(products);
        }else{
            products=recent.getProducts();
            Iterator<Product> iterator=products.iterator();
            while (iterator.hasNext()){
                if(iterator.next().equals(product)){
                    iterator.remove();
                }
            }
            products.addFirst(product);
            if(products.size()>4)
                products.removeLast();
            recent.setProducts(products);
        }

        return recent;
    }

    @Override
    public Long findPidByPNo(String pNo) {
        return productDao.findPidByPNo(pNo);
    }
}
